﻿using FineUI;
using SysManage.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using ZHDJ.Data.Models.Box;

namespace SysManage.Module.Menu
{
    public partial class PortalMenuList : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "PortalMenuView";
            }
        }

        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            // 权限检查
            CheckPowerWithButton("PortalMenuAdd", btnNew);

            btnNew.OnClientClick = Window1.GetShowReference("PortalMenuEdit.aspx", "新增菜单");

            BindGrid();
        }

        private void BindGrid()
        {
            List<PortalMenu> portalMenus = PortalMenuHelper.PortalMenus;
            Grid1.DataSource = portalMenus;
            Grid1.DataBind();
        }

        protected string GetModuleName(object moduleNameObj)
        {
            string moduleName = moduleNameObj.ToString();
            if (moduleName == "None")
            {
                return String.Empty;
            }
            return moduleName;
        }

        #endregion

        #region Events

        protected void Grid1_PreDataBound(object sender, EventArgs e)
        {
            // 数据绑定之前，进行权限检查
            CheckPowerWithWindowField("PortalMenuEdit", Grid1, "editField");
            CheckPowerWithLinkButtonField("PortalMenuDelete", Grid1, "deleteField");
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string menuID = GetSelectedDataKeyID(Grid1);

            if (e.CommandName == "Delete")
            {
                // 在操作之前进行权限检查
                if (!CheckPower("PortalMenuDelete"))
                {
                    CheckPowerFailWithAlert();
                    return;
                }

                var del = DB.PortalMenus.FirstOrDefault(m => m.ID == menuID);
                DB.PortalMenus.Remove(del);
                DB.SaveChanges();
                PortalMenuHelper.Reload();
                BindGrid();
            }
        }

        protected void Window1_Close(object sender, EventArgs e)
        {
            PortalMenuHelper.Reload();
            BindGrid();
        }

        #endregion
    }
}